﻿using System;
using System.Net;
using System.Xml.Serialization;
using AdRotator.Common;
#if SILVERLIGHT
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
#endif

namespace AdRotator.Silverlight.AdProviders
{
    public class PubCenterAd : AdProviderBase
#if SILVERLIGHT
        , IAdProviderNotManipulatingVisualTree
#endif
    {
        private string _appId, _adUnitId;
        private bool _isAutoRefresh = true;

        [XmlAttribute("AppId")]
        public string AppId
        {
            get
            {
                return _appId;
            }
            set
            {
                if (_appId != value)
                {
                    _appId = value;
                    NotifyPropertyChanged("AppId");
                }
            }
        }


        [XmlAttribute("AdUnitId")]
        public string AdUnitId
        {
            get
            {
                return _adUnitId;
            }
            set
            {
                if (_adUnitId != value)
                {
                    _adUnitId = value;
                    NotifyPropertyChanged("AdUnitId");
                }
            }
        }

        public override bool IsValid
        {
            get
            {
                return !String.IsNullOrEmpty(AppId) && !String.IsNullOrEmpty(AdUnitId);
            }
        }

        [XmlAttribute("IsAutoRefresh")]
        public bool IsAutoRefresh
        {
            get
            {
                return _isAutoRefresh;
            }
            set
            {
                if (_isAutoRefresh != value)
                {
                    _isAutoRefresh = value;
                    NotifyPropertyChanged("IsAutoRefresh");
                }
            }
        }

#if SILVERLIGHT
        private Microsoft.Advertising.Mobile.UI.AdControl _currentAdControl;
#endif

        public override void Initialize()
        {
#if SILVERLIGHT
            if (_currentAdControl != null)
            {
                _currentAdControl.AdRefreshed += new EventHandler(CurrentAdControl_AdRefreshed);
                _currentAdControl.ErrorOccurred += new EventHandler<Microsoft.Advertising.AdErrorEventArgs>(CurrentAdControl_ErrorOccurred);
            }
#endif
        }

        public override void Deinitialize()
        {
#if SILVERLIGHT
            if (_currentAdControl != null)
            {
                _currentAdControl.AdRefreshed -= new EventHandler(CurrentAdControl_AdRefreshed);
                _currentAdControl.ErrorOccurred -= new EventHandler<Microsoft.Advertising.AdErrorEventArgs>(CurrentAdControl_ErrorOccurred);
            }
#endif
        }

#if SILVERLIGHT
        public FrameworkElement CreateAd()
        {
            try
            {
                _currentAdControl = new Microsoft.Advertising.Mobile.UI.AdControl(
                    AppId,
                    AdUnitId,
                    IsAutoRefresh);
                _currentAdControl.Width = 480;
                _currentAdControl.Height = 80;
                RaiseLogEvent(String.Format("PubCenter ad with AppId {0} and AdUnitId {1} created with auto refresh {2}", AppId, AdUnitId, IsAutoRefresh ? "on" : "off"));
                return _currentAdControl;
            }
            catch (Exception ex)
            {
                RaiseLogEvent("Error occurred when creating PubCenter ad", ex);
                return null;
            }
        }

        private void CurrentAdControl_AdRefreshed(object sender, EventArgs e)
        {
            RaiseLogEvent("PubCenter ad successfully loaded/refreshed");
        }

        private void CurrentAdControl_ErrorOccurred(object sender, Microsoft.Advertising.AdErrorEventArgs e)
        {
            RaiseLogEvent(String.Format("Error occurred when loading PubCenter ad", e.Error));
            RaiseAdLoadingErrorEvent();
        }
#endif
    }
}
